<template>
  <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addRole, editRole, getRoleInfoId } from '@/api/setting'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹窗
    btnCancel() {
      // console.log('关闭')
      this.roleForm = {
        name: '',
        description: ''
      }
      // 重置表单
      this.$refs.roleForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async btnOK() {
      // console.log('确定')
      // 校验表单
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        // 编辑
        await editRole(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
      }
      // 通知父页面
      this.$emit('addRole')
      // 提醒用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    // 回显数据
    async getAddEdit(id) {
      // console.log(id)
      this.roleForm = await getRoleInfoId(id)
    }
  }
}
</script>

<style>

</style>
